package com.acme;
import java.io.IOException;

import com.acme.bankapp.service.*;
import com.acme.bankapp.domain.*;
import com.acme.bankapp.exceptions.*;

public class BankApplication {
	public static void main(String[] args) throws NotEnoughFundsException, IOException {
		
		ClientRegistrationListener[] listeners = new ClientRegistrationListener[3];
		
		listeners[0] = new EmailNotificationListener();
		listeners[1] = new PrintClientListener();
		
		Bank bank = new Bank(listeners, 10);
		if(args.length == 2 && "-loadfeed".equals(args[0])) {
			BankDataLoaderService.load(bank, args[1]);
		}
		if(args.length == 2 && "-loadbank".equals(args[0])) {
			try {
				bank = BankService.readBank(args[1]);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		
		
				
		Client client1 = new Client("Vasya", Gender.FEMALE);
		Account acc1 = new SavingAccount(60);
		client1.addAccount(acc1);
		try {
			bank.addClient(client1);
		} catch (ClientExistsException e1) {
			System.out.println(e1.getMessage());
			return;
		}
		
		Client client2 = new Client("Vat", Gender.MALE);
		Account acc2 = new SavingAccount(20);
		Account acc3 = new CheckingAccount(40, 20);
		client2.addAccount(acc2);
		System.out.println("decimal value: "+acc2.decimalValue());
		client2.addAccount(acc3);
		try {
			bank.addClient(client2);
		} catch (ClientExistsException e1) {
			System.out.println(e1.getMessage());
			return;
		}
		
		BankService.printBalance(bank);
		try {
			BankService.modifyBank(bank, 20);
		} catch (OverDraftLimitExceededException e) {
			System.out.println("ERROR!");
			System.out.println(e.getMessage());
			return;
		}
		
		try {
			BankService.saveBank(bank, "saving.txt");
		} catch (SavingDataFailure e) {
			System.out.println("Saving Data Failure");
		}
		
		BankService.printBalance(bank);
		BankService.printMaximumAmountToWithdraw(bank);
	}
}
